package view.components;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import filter.IFilter;
import view.GedivaGUI;

/**
 * The collection of commands represented as buttons that apply to the active
 * image.
 * 
 * @author Vijay Ram
 */
@SuppressWarnings("serial")
public class CheckBoxPanel extends JPanel
{
    private GedivaGUI myGUI;

    /**
     * Create panel that will update the given view.
     */
    public CheckBoxPanel(GedivaGUI gedivaGUI)
    {
        myGUI = gedivaGUI;
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.setPreferredSize(new Dimension(200, 40));
    }

    /**
     * Add the given Command as a button with the given name.
     */
    public void add(final IFilter<?, ?> iFilter)
    {
        JCheckBox box = new JCheckBox(iFilter.toString());
        box.addActionListener(new ActionListener()
        {
            private boolean isChecked = false;

            public void actionPerformed(ActionEvent e)
            {
                if (isChecked)
                {
                    myGUI.removeFilter(iFilter);
                }
                else
                {
                    myGUI.applyFilter(iFilter);
                }
                isChecked = !isChecked;
            }
        });
        this.add(box);

    }

    /**
     * adds command that does nothing
     * 
     * @param name
     */
    public void add(String name)
    {
        this.add(new JCheckBox(name));
    }
}